package com.jankins.pat;

import java.util.Scanner;
import java.util.*;

/**
 * TopologicalOrder
 */
public class TopologicalOrder {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Digraph g = new Digraph(in);

        int t = in.nextInt();
        List<Integer> result = new ArrayList<Integer>();
        List<Integer> seq;
        for(int i = 0; i < t; i++) {
            seq = new ArrayList<Integer>();
            for(int j = 0; j < g.V; j++) {
                int v = in.nextInt();
                seq.add(v);
            }
            if(!isTopo(g, seq)) result.add(i); 
        }

        for(int i = 0; i < result.size(); i++) {
            System.out.print(result.get(i));
            if(i < result.size()) 
                System.out.print(" ");
        }
        System.out.println();
    }

    public static boolean isTopo(Digraph g, List<Integer> seq) {
        int[] indegree = Arrays.copyOf(g.indegree, g.V + 1);
        
        for(int w : seq) {
            if(indegree[w] != 0) return false;
            for(int v : g.edges[w]) {
                indegree[v]--;
            }
        }
        return true;
    }

    private static class Digraph{
        int V;
        int E;
        List<Integer>[] edges;
        int[] indegree;

        @SuppressWarnings("unchecked")
        public Digraph(Scanner in) {
            V = in.nextInt();
            E = in.nextInt();

            edges = (List<Integer>[]) new List[V + 1];
            indegree = new int[V + 1];
            for(int i = 1; i < V + 1; i++) {
                edges[i] = new LinkedList<Integer>();
            }

            for(int i = 0; i < E; i++) {
                int from = in.nextInt();
                int to = in.nextInt();
                edges[from].add(to);
                indegree[to]++;
            }
        }
    }
}